<?php
/*
    Required variables set
    $db = $mysql-Database(or MariaDB)
    $icon = icon file-name insice icons/22/ folder
    $loc_entity = nice localized name for the entity listed here
    $loc_entities = $loc_entity -> plural
    $typ = type shorthand(one char)
    $table = table to querry assumed to have id, name and retired field(also used for translation e.g.: RLang("edit".$table))
    $last = last selected id
    $last_parentt = last known parent selection
    $parent_id = field name of foreign key(optional)
    $filter = optional filter variable for GET call to maintime.php
    $group = classname for Group-Table
    $selectf = javascript select function-call
    $lvl_links = Array off level and link array. example below.

$lvl_links  = 
[ 
    "edit" => ["link" => "edit_task.php"], ["level" => LEVEL_EDIT_TASK],
    "retire" => ["link" => "retire_task.php"], ["level" => LEVEL_RETIRE_TASK],
    "create" => ["link" => "edit_task.php"], ["level" => LEVEL_CREATE_TASK],
    "unretire" => ["link" => "unretire_task.php"], ["level" => LEVEL_UNRETIRE_TASK]
];
*/
?>

<?php
//
function CreateSubListBlock($db, $icon, $loc_entity, $loc_entities, $typ, $table, $last, $last_parent, $parent_id, $group, $lvl_links, $selectf, $filter)
{
    echo ("<div class=\"BorderBox\">");

    printf("<img src='icons/22/%s'> %s", $icon, $loc_entities);
    if ($_SESSION['userlevel'] >= $lvl_links["create"]["level"]) {
        printf("<a class='tooltip' href='%s?id=-1'><img src='icons/22/list-add.png'><div class='rtooltiptext'>%s</div></a>", $lvl_links["create"]["link"], sprintf(RLang("create_new"), $loc_entity));
    }
    $remote_present = isset($parent_id) && !is_null($parent_id);
    echo ("<div class=\"SubList\">");
    if ($_SESSION['userlevel'] >= $lvl_links["unretire"]["level"]) {

        if ($remote_present)
            $sql = "SELECT name, id, $parent_id, retired FROM $table order by $parent_id";
        else
            $sql = "SELECT name, id, retired FROM $table order by id";
    } else {
        if ($remote_present)
            $sql = "SELECT name, id, $parent_id, retired FROM $table where retired is null order by $parent_id";
        else
            $sql = "SELECT name, id, retired FROM task where retired is null order by id";
    }

    if (!$result = get_db_result($db, $sql, null)) {
        echo ($sql . " ");
        die(mysqli_error($db));
    }



    # Interating through all task works only if they are ordered by project_id
    $currentRemote = null;
    $rowcnt = mysqli_num_rows($result);
    $lastindex = $rowcnt - 1;
    for ($i = 0; $i < $rowcnt; $i++) {
        $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
        $id = $row['id'];
        $name = $row['name'];
        //if there is no remote set it to $last_parent wich should be nagative in such case
        $rid = $remote_present ? $row[$parent_id] : $last_parent;

        # currentProject is not null and not like $rid means => close current div and start new one
        if ($currentRemote != null && $currentRemote != $rid) {
            echo ("</table>\r\n");
            $currentRemote = null;
        }
        # currentProject is null means first element => just new div open
        if ($currentRemote == null) {
            echo $currentRemote;
            printf("<table class=\"DataTable %s\" id='%sg%s' width=\"100%%\" %s>\r\n\t\t", $group, $typ, $rid, ($rid == $last_parent ? "" : "style=\"display:none\""));
            if (isset($filter))
                echo ("<col width='22'>\r\n\t\t");
            echo ("<col width='auto'>\r\n\t\t");
            echo ("<col width='22'>\r\n\t\t");
            echo ("<col width='22'>\r\n\t\t");
            $currentRemote = $rid;
        }
        echo ("<tr>\r\n\t\t\t");
        if (isset($filter))
            printf("<td>\r\n\t\t\t\t<a class='tooltip' href='maintime.php?%s=%s'><img src='icons/22/view-filter.png'><div class='rtooltiptext'>%s</div></a>\r\n\t\t\t</td>\r\n\t\t\t", $filter, $id, sprintf(RLang("filter_entity"), $loc_entity));
        echo ("<td>\r\n\t\t\t\t");
        if ($last == $id || ($last == 0 && $i == 0))
            printf("<input type=\"radio\" id=\"%s%s\" onclick=\"%s(%s)\" name=\"%s\" class=\"%s\" value=\"%s\" checked=\"checked\">", $typ, $id, $selectf, $id, $table, $table, $id);
        else
            printf("<input type=\"radio\" id=\"%s%s\" onclick=\"%s(%s)\" name=\"%s\" class=\"%s\" value=\"%s\">", $typ, $id, $selectf, $id, $table, $table, $id);
        echo ($name . "\r\n\t\t\t");
        echo ("</td>");
        if ($row['retired'] == null) {
            if ($_SESSION['userlevel'] >= $lvl_links["retire"]["level"])
                printf("\r\n\t\t\t<td>\r\n\t\t\t\t<a class='tooltip' href='%s?id=%s'><img src='icons/22/list-remove.png'>
                              <div class='ltooltiptext'>%s</div></a>\r\n\t\t\t</td>\r\n\t\t\t", $lvl_links["retire"]["link"], $id, RLang("retire" . $table));
            if ($_SESSION['userlevel'] >= $lvl_links["edit"]["level"])
                printf("<td>\r\n\t\t\t\t<a class='tooltip' href='%s?id=%s'><img src='icons/22/document-edit.png'>
                              <div class='ltooltiptext'>%s</div></a>\r\n\t\t\t</td>", $lvl_links["edit"]["link"], $id, RLang("edit" . $table));
        } else if ($_SESSION['userlevel'] >= $lvl_links["unretire"]["level"]) {
            printf("<td>\r\n\t\t\t\t<a class='tooltip' href='%s?id=%s'><img src='icons/22/view-refresh.png'>
                           <div class='ltooltiptext'>%s</div></a>\r\n\t\t\t</td>", $lvl_links["unretire"]["link"], $id, RLang("unretire" . $table));
        }
        echo ("\r\n\t\t</tr>\r\n\t");
        #if last element (index check) close current div
        if ($i == $lastindex) {
            echo ("</table>\r\n");
        }
    }

    echo ("</div>\r\n</div>");
}

?>


